// 获取当前登录用户信息接口
export default defineEventHandler(async (event) => {
  const authHeader = getHeader(event, 'authorization')
  
  // 模拟延迟
  await new Promise(resolve => setTimeout(resolve, 300))

  // 验证 token
  if (!authHeader || !authHeader.startsWith('Bearer ')) {
    throw createError({
      statusCode: 401,
      message: '未登录或 token 无效'
    })
  }

  const token = authHeader.substring(7) // 移除 'Bearer ' 前缀

  if (!token.startsWith('token_')) {
    throw createError({
      statusCode: 401,
      message: 'token 格式错误'
    })
  }

  // 从 token 中提取用户ID（模拟）
  const parts = token.split('_')
  const userId = parts[1]

  if (!userId) {
    throw createError({
      statusCode: 401,
      message: 'token 格式错误'
    })
  }

  // 模拟用户数据
  const users: Record<string, any> = {
    '1': {
      id: '1',
      username: 'admin',
      email: 'admin@example.com',
      avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=admin',
      role: 'admin',
      bio: '系统管理员',
      phone: '13800138000',
      createdAt: '2024-01-01T00:00:00.000Z'
    },
    '2': {
      id: '2',
      username: 'testuser',
      email: 'test@example.com',
      avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=test',
      role: 'user',
      bio: '测试用户',
      phone: '13900139000',
      createdAt: '2024-01-15T00:00:00.000Z'
    }
  }

  const user = users[userId]

  if (!user) {
    throw createError({
      statusCode: 404,
      message: '用户不存在'
    })
  }

  return {
    code: 200,
    message: '获取成功',
    data: user
  }
})

